package com.dangz.dans.controller;

import com.dangz.dans.entity.CommonResponse;
import com.dangz.dans.model.wx.SubscribeInfo;
import com.dangz.dans.response.ApiEnum;
import com.dangz.dans.response.CodeEnum;
import com.dangz.dans.response.TypeEnum;
import com.dangz.dans.utils.IPUtils;
import com.dangz.dans.utils.RedisUtil;
import com.dangz.dans.utils.WxUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author dangz
 * @since 2018/1/30 15:27
 */
@RestController
@RequestMapping("dans/wx_test")
@Api(tags = "微信测试")
public class WxTestController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RedisUtil redisUtil;
    private final String appid = "wx0998c6284abdd630";
    private final String secret = "d4624c36b6795d1d99dcf0547af5443d";
    @ApiOperation("测试")
    /*@ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String")
    })*/
    @GetMapping(value = "i")
    public CommonResponse findOne(HttpServletRequest request){
        logger.info("IP:{}", IPUtils.getIpAddr(request));
        CommonResponse commonResponse = null;
        try {
            List<SubscribeInfo> openids = WxUtils.getOpenIdInfo(appid, secret, redisUtil, "");
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                    ApiEnum.XTGL, CodeEnum.SUCCESS_SERVICE_LOCAL), CodeEnum.SUCCESS_SERVICE_LOCAL.getInfo(), openids);
        } catch (Exception e) {
            e.printStackTrace();
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Error,
                    ApiEnum.XTGL, CodeEnum.FAIL_SERVICE_LOCAL), CodeEnum.FAIL_SERVICE_LOCAL.getInfo());
        }
        return commonResponse;
    }
}
